package cn.iocoder.yudao.module.alipay.service.cpsactivitytype;

import cn.iocoder.yudao.module.alipay.util.TreeUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.cpsactivitytype.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpsactivitytype.CpsActivityTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.cpsactivitytype.CpsActivityTypeConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.cpsactivitytype.CpsActivityTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * cps活动分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CpsActivityTypeServiceImpl implements CpsActivityTypeService {

    @Resource
    private CpsActivityTypeMapper cpsActivityTypeMapper;

    @Override
    public Long createCpsActivityType(CpsActivityTypeCreateReqVO createReqVO) {
        // 插入
        CpsActivityTypeDO cpsActivityType = CpsActivityTypeConvert.INSTANCE.convert(createReqVO);
        cpsActivityTypeMapper.insert(cpsActivityType);
        // 返回
        return cpsActivityType.getId();
    }

    @Override
    public void updateCpsActivityType(CpsActivityTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validateCpsActivityTypeExists(updateReqVO.getId());
        // 更新
        CpsActivityTypeDO updateObj = CpsActivityTypeConvert.INSTANCE.convert(updateReqVO);
        cpsActivityTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCpsActivityType(Long id) {
        // 校验存在
        validateCpsActivityTypeExists(id);
        // 删除
        cpsActivityTypeMapper.deleteById(id);
    }

    private void validateCpsActivityTypeExists(Long id) {
        if (cpsActivityTypeMapper.selectById(id) == null) {
            throw exception(CPS_ACTIVITY_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public CpsActivityTypeDO getCpsActivityType(Long id) {
        return cpsActivityTypeMapper.selectById(id);
    }

    @Override
    public List<CpsActivityTypeDO> getCpsActivityTypeList(Collection<Long> ids) {
        return cpsActivityTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CpsActivityTypeDO> getCpsActivityTypePage(CpsActivityTypePageReqVO pageReqVO) {
        return cpsActivityTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CpsActivityTypeDO> getCpsActivityTypeList(CpsActivityTypeExportReqVO exportReqVO) {
        return cpsActivityTypeMapper.selectList(exportReqVO);
    }

    @Override
    public List<BaseTreeObj> getTreeTypeList(CpsActivityTypeExportReqVO reqVO) {
        List<CpsActivityTypeDO> infoTypeDOS = cpsActivityTypeMapper.selectList(reqVO);
        List<BaseTreeObj> baseTreeObjs = new ArrayList<>();
        for (CpsActivityTypeDO infoTypeDO :
                infoTypeDOS) {
            BaseTreeObj baseTreeObj = new BaseTreeObj();
            baseTreeObj.setId(infoTypeDO.getId());
            baseTreeObj.setValue(String.valueOf(infoTypeDO.getId()));
            baseTreeObj.setLabel(infoTypeDO.getName());
            baseTreeObj.setParentId(infoTypeDO.getParentId());
            baseTreeObj.setCode(infoTypeDO.getActCode());
            baseTreeObj.setLevel(infoTypeDO.getTypeLevel());
            baseTreeObjs.add(baseTreeObj);
        }
        return TreeUtil.list2TreeConverter(baseTreeObjs, 0L);
    }

    @Override
    public List<CpsActivityTypeDO> getTypeList(CpsActivityTypeExportReqVO reqVO) {
        return cpsActivityTypeMapper.selectList(reqVO);
    }

}
